<?php

namespace backend\modules\supplier\models;

use Yii;

/**
 * 这个模型基于表 "{{%s_storage_slot}}" 生成。
 *
 * @property integer $id
 * @property integer $supplier_id
 * @property integer $storage_id
 * @property integer $rack_id
 * @property string $name
 * @property integer $sort
 * @property integer $length
 * @property integer $width
 * @property integer $height
 */
class Slot extends \common\models\AR
{
    public $modelName='';
    
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%s_storage_slot}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['supplier_id', 'storage_id', 'rack_id', 'name'], 'required'],
            [['supplier_id', 'storage_id', 'rack_id', 'sort', 'length', 'width', 'height'], 'integer'],
            [['name'], 'string', 'max' => 100]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => '货位ID',
            'supplier_id' => '供应商ID',
            'storage_id' => '所属仓库',
            'rack_id' => '所属货架',
            'name' => '名称',
            'sort' => '排序',
            'length' => '长(CM)',
            'width' => '宽(CM)',
            'height' => '高(CM)',
        ];
    }
        
    public function beforeValidate() {
        if(parent::beforeValidate()){
            $this->supplier_id = Yii::$app->user->identity->supplier_id;
            return true;
        }else{
            return FALSE;
        }
    }
    
    /**
     * 引用仓库信息
     * @return Storage
     */
    public function getStorage(){
        return $this->hasOne(Storage::className(), ['id' => 'storage_id']);
    }

    /**
     * 引用货架信息
     * @return Rack
     */
    public function getRack(){
        return $this->hasOne(Rack::className(), ['id' => 'rack_id']);
    }
    
}
